package com.calque;

import java.io.InputStream;
import java.util.ArrayList;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.widget.Toast;

import com.activity.MyApplicationMainActivity;
import com.commun.Param;
import com.commun.Utils;
import com.commun.Utils.CalqueEnum;
import com.geo.GeoTools;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.object.*;
import com.route.Road;
import com.route.RoadProvider;

public class CalqueSitesHavePicture extends ItemizedOverlay<OverlayItemCustom> {

	/* Declration */
	private List<OverlayItemCustom> items = new ArrayList<OverlayItemCustom>();

	private Drawable marker = null;
	private Context context = null;
	OverlayItemCustom overlayItem;
	MapView mapView;
	public static Toast myToast;
	private Handler mHandler = new Handler();
	private Runnable mUpdateTimeTask = new Runnable() {
		public void run() {
			Utils.DisparitonLayoutImageTaost(context);
		}
	};

	/* Overlay Methodes */

	private static CalqueSitesHavePicture instance;

	public static CalqueSitesHavePicture getInstance() {

		return instance;
	}

	public static void InitialisationInsatance(Drawable marker,
			Context context, MapView mapView) {

		instance = new CalqueSitesHavePicture(marker, context, mapView);
	}

	private CalqueSitesHavePicture(Drawable marker, Context context,
			MapView mapView) {
		super(marker);
		this.marker = marker;
		this.context = context;
		this.mapView = mapView;
		// DomFeedParser domFeedParser = new DomFeedParser(Param.URI);

		 //Renitialise() ;

	}

	public void Renitialise() {
		items = new ArrayList<OverlayItemCustom>();
		for (Mosque mosquai : Utils.getMosquais())
			if (mosquai.getHavePicture()) {
				OverlayItemCustom item = new OverlayItemCustom(GeoTools
						.makeGeoPoint(mosquai.getLatitude(), mosquai
								.getLongitude()), mosquai.getInfo(), mosquai
						.getNom());
				item.setMosquai(mosquai);

				int resID = ((Activity) context).getResources().getIdentifier(
						mosquai.getPictureName(), "drawable",
						((Activity) context).getPackageName());
				item.setResImageMosque(resID);

				items.add(item);

			}
		populate();
	}

	@Override
	protected OverlayItemCustom createItem(int i) {
		return (items.get(i));
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		if (MyApplicationMainActivity.getMyMapView().getZoomLevel() < 8) {
			populate();
		} else
			super.draw(canvas, mapView, false);
		boundCenterBottom(marker);

		if (!items.isEmpty()
				&& Utils.geoPointsAreSame(items.get(nbTaped).getPoint(),
						mapView.getMapCenter()) && animationLaunched) {

			String text = items.get(nbTaped).getTitle() + " "
					+ items.get(nbTaped).getSnippet();

			Utils.MyCustomisedViewToast(context, text, items.get(nbTaped)
					.getResImageMosque());

			mHandler.removeCallbacks(mUpdateTimeTask);
			mHandler.postDelayed(mUpdateTimeTask, Param.LONG_DELAY);

			animationLaunched = false;
			Utils.calqueEnum = CalqueEnum.HAVE_PICTURE_TEXT;
			initialiseRoute(canvas,mapView);
		}
		if (!items.isEmpty() && drawRoute&& CalqueSites.isDrawingRouteActive ) Utils.drawPathRoute(mapView, canvas,mPoints);

	}

	

	//Route intitialisation	

		private ArrayList<GeoPoint> mPoints;
		private Road mRoad;
		public static boolean drawRoute = false;
		Handler mHandler2 = new Handler() {
			public void handleMessage(android.os.Message msg) {

				if (mRoad.mRoute.length > 0) {
					mPoints = new ArrayList<GeoPoint>();
					for (int i = 0; i < mRoad.mRoute.length; i++) {
						mPoints.add(new GeoPoint((int) (mRoad.mRoute[i][1] * 1000000),
								(int) (mRoad.mRoute[i][0] * 1000000)));
					}
				}
				
				drawRoute = true;
			};
		};
		private void initialiseRoute(Canvas canvas, MapView mapView){
			Utils.setDrawRouteBooleanFalse();
			configRoute();
			
		}
		
		private void configRoute(){
			new Thread() {
				@Override
				public void run() {
					double fromLat =MyApplicationMainActivity.mLatitude , fromLon = MyApplicationMainActivity.mLongitude, toLat = items.get(nbTaped).getMosquai().getLatitude() , toLon = items.get(nbTaped).getMosquai().getLongitude();
					String url = RoadProvider
							.getUrl(fromLat, fromLon, toLat, toLon);
					InputStream is = Utils.getConnection(url);
					mRoad = RoadProvider.getRoute(is);
					mHandler2.sendEmptyMessage(0);
					
				}
			}.start();
		}
		
		

	//end route inti
		
	private boolean animationLaunched = false;

	private int nbTaped;

	@Override
	protected boolean onTap(int i) {

		mapView.getController().animateTo(items.get(i).getPoint());
		animationLaunched = true;
		nbTaped = i;

		return (true);
	}

	@Override
	public int size() {

		return (items.size());
	}

	public void Populate() {
		populate();
	}

}